import { game } from "../../../../script/game/control/game";
import { assetsBase, ItemBase } from "../../../../script/game/control/Item/ItemBase";
import { assetsType, itemType } from "../../../../script/game/gameType";
import { ccs } from "../../../../script/util/services";
import { WinBase } from "../../../../script/wins/WinBase";

const {ccclass, property} = cc._decorator;

@ccclass
export default class NewClass extends WinBase {

    @property(cc.Label)
    label: cc.Label = null;
    bagAssets: assetsBase;
    assetsItem: assetsType;
    cost: number = 0;
    buyMode: string = "month";
    start () {
        this.assetsItem = ccs.dataMgr.getAssetsById(this._args.id);
        this.nodes.itemName.getComponent(cc.Label).string = this.assetsItem.name;
        //是否拥有 从背包获取
        this.bagAssets = game.playerMgr.bag.assetsBag.getItem(this._args.id);
        if(this.bagAssets) {
            //出售按钮
            this.changeBtnState(1);
            this.cost = (this.bagAssets.data.price - this.bagAssets.data.leftMoney) * 0.7;
        } else {
            this.changeBtnState(0);
            this.cost = this.assetsItem.firstPay;
        }
        
        this.nodes.cost.getComponent(cc.Label).string = "总价：" + ccs.gf.getOmitNum(this.assetsItem.price)  + "元";
        this.nodes.count.getComponent(cc.Label).string = ccs.gf.getOmitNum(this.cost)  + "元";

        this.touchEndEventScale(this.nodes.reduce, () => {
            this.cost -= this.assetsItem.price * 0.1;
            this.limitCount();
        })
        this.touchEndEventScale(this.nodes.add, () => {
            this.cost += this.assetsItem.price * 0.1;
            this.limitCount();
        })
        this.touchEndEventScale(this.nodes.ensureBuy, () => {
            this.buy();
        })
        this.touchEndEventScale(this.nodes.equip, () => {
            game.playerMgr.equipAssets(this.assetsItem, this.assetsItem.type == "car"? "traffic": this.assetsItem.type);
            game.recordMgr.addList({name: "你", action: `更换了${this.assetsItem.name}`});
            ccs.winMgr.showWin("toast", {msg: "更换成功"});
            this._close();
        })
        this.touchEndEventScale(this.nodes.allBuy, () => {
            this.buyMode = "all";
            this.nodes.operation.active = false;
            this.cost = this.assetsItem.price;
            this.nodes.count.getComponent(cc.Label).string = ccs.gf.getOmitNum(this.cost)  + "元";
           
            if(this.assetsItem.price >= 300000) {
                game.playerMgr.achieveFinish("richCar", 1);
            }
        })
        this.touchEndEventScale(this.nodes.monthBuy, () => {
            this.buyMode = "month";
            this.cost = this.assetsItem.firstPay;
            this.nodes.count.getComponent(cc.Label).string = ccs.gf.getOmitNum(this.cost)  + "元";
            this.nodes.operation.active = true;
            game.playerMgr.achieveFinish("car", 1);
            if(this.assetsItem.price >= 300000) {
                game.playerMgr.achieveFinish("richCar", 1);
            }
        })
        this.touchEndEventScale(this.nodes.sale, () => {
            let assets: assetsType = this.bagAssets.data;
            //删除
            game.playerMgr.bag.assetsBag.delItem(assets.id);
            game.playerMgr.heroInfo.money += this.cost;
            this.nodes.operation.active = true;
            ccs.winMgr.showWin("toast", {msg: "成功出售，收入+" + this.cost});
            game.recordMgr.addList({name: "你", action: `卖了 ${this.assetsItem.name}，赚了${ccs.gf.getOmitNum(this.cost)}元`,location: "4s店"});
            //减少支出，增加收入
            let pay = assets.monthCost;
            if(assets.month > 0) {
                pay += assets.monthPay * (1 + assets.interest);
            }
            if(game.playerMgr.heroInfo.traffic && game.playerMgr.heroInfo.traffic.id == assets.id) {
                game.playerMgr.heroInfo.traffic = null;
            }
            if(game.playerMgr.heroInfo.house && game.playerMgr.heroInfo.house.id == assets.id) {
                game.playerMgr.heroInfo.house = null;
            }
            game.playerMgr.calPay(-pay, 0, 0, this.cost);
            ccs.mainEmitter.emit("updateAssets");
            ccs.mainEmitter.emit("updateAttribute");
            ccs.mainEmitter.emit("updateInfo");
            this._close();
        })
    }
    changeBtnState(have: number) {
        if(have) {
            this.nodes.have.active = true;
            this.nodes.sale.active = true;
            this.nodes.allBuy.active = false;
            this.nodes.monthBuy.active = false;
            this.nodes.ensureBuy.active = false;
            this.nodes.ensureSale.active = true;
            this.nodes.equip.active = true;
            //增加按钮 
            this.nodes.operation.active = false;
        }else {
            this.nodes.have.active = false;
            this.nodes.sale.active = false;
            this.nodes.allBuy.active = true;
            this.nodes.monthBuy.active = true;
            this.nodes.ensureBuy.active = true;
            this.nodes.ensureSale.active = false;
            this.nodes.operation.active = true;
        }
    }
    limitCount() {
        if(this.cost < 0 ) {
            this.cost = 0;
        }else if(this.cost <= this.assetsItem.firstPay) {
            this.cost = this.assetsItem.firstPay;
        }else if(this.cost >= this.assetsItem.price) {
            this.cost = this.assetsItem.price * 0.9;
        }
        // this.nodes.cost.getComponent(cc.Label).string = this.cost.toString();
        this.nodes.count.getComponent(cc.Label).string = ccs.gf.getOmitNum(this.cost)  + "元";

    }
    onSlider(tar) {
        let num = tar.progress;
        this.cost = num * this.assetsItem.price;
        this.limitCount();
    }
    buy() {
        //查看钱
        if(game.playerMgr.heroInfo.money >= this.cost ) {
            game.playerMgr.heroInfo.money -= this.cost;
            let assetsPay = this.assetsItem.monthCost;
            
            if(this.buyMode === "month") {
                this.assetsItem.leftMoney = this.assetsItem.price - this.cost;
                assetsPay += this.assetsItem.monthPay * (this.assetsItem.interest + 1);
                game.recordMgr.addList({name: "你", action: `购买了${this.assetsItem.name}， 别忘了每个月按时还贷`, effect: `金钱 - ${this.cost}，每月支出增加${assetsPay}`});

            } else {
                this.assetsItem.month = 0;
                this.assetsItem.leftMoney = 0;
                game.recordMgr.addList({name: "你", action: `购买了${this.assetsItem.name}`, effect: `金钱 - ${this.cost}，每月支出增加${this.assetsItem.monthCost}`});

            }
            game.playerMgr.achieveFinish("car", 1);
            game.playerMgr.bag.assetsBag.addItem(new assetsBase(this.assetsItem, 1));
            ccs.winMgr.showWin("toast", { msg: "购买成功" });
            //增加支出
            game.playerMgr.calPay(assetsPay, 0, this.cost);
            ccs.mainEmitter.emit("updateAttribute");
            ccs.mainEmitter.emit("updateAssets");
            ccs.mainEmitter.emit("updateInfo");
            ccs.mainEmitter.emit("updateMoney");
            
            this._close();
        } else {
            ccs.winMgr.showWin("toast", { msg: "钱包有点羞涩" });
            
        }
        cc.log(game.playerMgr.bag.assetsBag);
    }
}
